
package net.swa.dewei.web.action;

import java.io.BufferedOutputStream;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Required;
import net.swa.dewei.beans.entity.DingDan;
import net.swa.dewei.beans.vo.GroupAndMaxId;
import net.swa.dewei.service.DingDanService;
import net.swa.system.beans.entity.User;
import net.swa.system.service.ICommonService;
import net.swa.system.util.SysConst;
import net.swa.system.util.annotation.Log;
import net.swa.system.web.action.AbstractBaseAction;
import net.swa.util.JsonResult;

/**订单信息表 Action**/
public class DingDanAction extends AbstractBaseAction
{
    private static final long serialVersionUID = 6248407300486551422L;

    private DingDanService dingDanService;

    private DingDan model;

    //实体类id
    private Long id;

    //门店id
    private Long shopId;

    //批量删除的id集合
    private Long[] ids;

    private String status;

    //用户类型
    private Short userType;

    /**分页中订单id**/
    private String dingDanUid;

    /**订单状态***/
    private String state;

    /**更换方式***/
    private String changeType;

    private Long fuwuZhanId;

    /**当前用户若是服务站，其服务站id**/
    private Long fuwuzhanId;

    //客户名
    private String customerName;

    //联系方式、
    private String customerTel;

    //门店名称
    private String mendianName;

    private String fuwuzhanName;

    /** 公用方法接口 */
    private ICommonService commonService;

    @Required
    @Resource
    public void setCommonService(ICommonService commonService)
    {
        this.commonService = commonService;
    }

    public Long getId()
    {

        return id;
    }

    public void setId(Long id)
    {
        this.id = id;
    }

    public Long[] getIds()
    {
        return ids;
    }

    public void setIds(Long[] ids)
    {
        this.ids = ids;
    }

    public DingDan getModel()
    {
        return model;
    }

    public void setModel(DingDan model)
    {
        this.model = model;
    }

    @Required
    @Resource
    public void setDingDanService(DingDanService dingDanService)
    {
        this.dingDanService = dingDanService;
    }

    /**初始化分页查询(支持不同数据范围查询)**/
    @Log(description = "初始化分页查询(支持不同数据范围查询)")
    public String initQuery()
    {
//        User user = getCurrentUser();
        return "queryList";
    }

    /**
     * @throws Exception */
    public String queryList() throws Exception
    {
        currentPage = currentPage == 0 ? 1 : currentPage;
        pageSize = pageSize == 0 ? 20 : pageSize;
        JsonResult<DingDan> json = null;
        User user = getCurrentUser();
        if (null != user)
        {
            if (SysConst.USER_TYPE_FUWUZHAN == user.getUserType())
            {
                fuwuZhanId = user.getFuwuzhanId();
            }
            else
            {
                fuwuZhanId = null;
            }
        }
        json = dingDanService.queryDingdansPage3(customerName, customerTel, mendianName, dingDanUid, state, changeType,
                fuwuZhanId, fuwuzhanName, currentPage, pageSize, orderBy, orderType);
        //  json = dingDanService.queryDingdansPage2(customerName, customerTel, mendianName, dingDanUid, state, changeType, fuwuZhanId, currentPage, pageSize);

        json.setCurrentPage(currentPage);
        outJson(json);
        return null;
    }

    public String queryGroup() throws Exception
    {
        Map<String, ?> map = dingDanService.queryGroup(fuwuzhanId);
        outJson(map);
        return null;
    }

    /**
     * 服务站or管理员查询订单
     * 平板端接口
     * @throws Exception */
    public String queryDingdans() throws Exception
    {
        currentPage = currentPage == 0 ? 1 : currentPage;
        pageSize = pageSize == 0 ? 20 : pageSize;
        JsonResult<DingDan> json = null;
        json = dingDanService.queryDingdansPage2(customerName, customerTel, mendianName, dingDanUid, state, changeType,
                fuwuZhanId, currentPage, pageSize);
        json.setCurrentPage(currentPage);
        outJson(json);
        return null;
    }

    /**去新增或者修改页面**/
    @Log(description = "去新增或者修改订单页面")
    public String toAddDingDan() throws Exception
    {
        User user = getCurrentUser();
        if (null != user)
        {
            userType = user.getUserType();
            if (null != id)
            {
                model = dingDanService.queryById(id);
            }
            else
            {
                model = new DingDan();
            }
            return "toAddDingDan";
        }
        else
        {
            return LOGIN;
        }
    }

    /**新增或者修改**/
    public String saveDingDan() throws Exception
    {
        if (null == model.getId())
        {
            //            dingDanService.add(model);
        }
        else
        {
            //更改订单状态
            if (dingDanService.updateState(model))
            {
                outSuccess();
            }
            else
            {
                outError("error");
            }
        }

        return null;
    }

    /** * 删除方法**/
    @Log(description = "删除订单")
    public String delete() throws Exception
    {
        dingDanService.delete(ids);
        outSuccess();
        return null;
    }

    /**更改订单状态
     * @throws Exception */
    public String updateOrderStatus() throws Exception
    {
        if (null == id || StringUtils.isBlank(status))
        {
            outError("参数不合法");
        }
        else
        {
            if (dingDanService.updateOrderStatus(id, status))
            {
                outSuccess();
            }
            else
            {
                outError("Error");
            }
        }
        return null;
    }

    public String assign() throws Exception
    {
        if (null == id || null == shopId)
        {
            outError("参数不合法");
        }
        else
        {
            if (dingDanService.updateAssign(id, shopId))
            {
                outSuccess();
            }
            else
            {
                outError("Error");
            }

        }
        return null;
    }

    /**
     * 指派 信息
     * @return
     */
    public String zhipai()
    {
        User u = getCurrentUser();
        if (null != u)
        {
            if (SysConst.USER_TYPE_FUWUZHAN == u.getUserType())
            {
                if (null != u.getFuwuzhanId())
                {
                    fuwuzhanId = u.getFuwuzhanId();
                }
            }
            return "zhipai";
        }
        else
        {
            return LOGIN;
        }

    }

    public String querytest()
    {
        String f1= getRequestParameter("fid");
        String f2= getRequestParameter("maxId");
        Long id =f1==null?null: Long.parseLong(f1);
        Long maxId = f2==null?null: Long.parseLong(f2);

        GroupAndMaxId v = dingDanService.queryGroupMax(id, maxId,"0");

        System.out.println("XXXXX　　"+v);
        return null;
    }

    //导出跟踪记录的excel
    public String exportExcel() throws Exception
    {
        User user = getCurrentUser();
        if (SysConst.USER_TYPE_FUWUZHAN == user.getUserType())
        {
            fuwuZhanId = user.getFuwuzhanId();
        }
        else
        {
            fuwuZhanId = null;
        }
        HSSFWorkbook workbook = dingDanService.exportExcel(dingDanUid, state, changeType, fuwuZhanId);
        HttpServletResponse response = getResponse();
        BufferedOutputStream bos = null;
        try
        {
            response.setContentType("application/vnd.ms-excel");
            String filename = new String("filename".getBytes("gbk"), "iso-8859-1") + ".xls";
            response.setHeader("Content-disposition", "attachment;filename=" + filename);
            bos = new BufferedOutputStream(response.getOutputStream());
            workbook.write(bos);
            bos.flush();
            removeSessionAttribute("listSheet");
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            bos.close();
        }
        return null;
    }

    public String getStatus()
    {
        return status;
    }

    public void setStatus(String status)
    {
        this.status = status;
    }

    public Long getShopId()
    {
        return shopId;
    }

    public void setShopId(Long shopId)
    {
        this.shopId = shopId;
    }

    public String getDingDanUid()
    {
        return dingDanUid;
    }

    public void setDingDanUid(String dingDanUid)
    {
        this.dingDanUid = dingDanUid;
    }

    public String getState()
    {
        return state;
    }

    public void setState(String state)
    {
        this.state = state;
    }

    public String getChangeType()
    {
        return changeType;
    }

    public void setChangeType(String changeType)
    {
        this.changeType = changeType;
    }

    public ICommonService getCommonService()
    {
        return commonService;
    }

    public Long getFuwuZhanId()
    {
        return fuwuZhanId;
    }

    public void setFuwuZhanId(Long fuwuZhanId)
    {
        this.fuwuZhanId = fuwuZhanId;
    }

    public Short getUserType()
    {
        return userType;
    }

    public void setUserType(Short userType)
    {
        this.userType = userType;
    }

    public Long getFuwuzhanId()
    {
        return fuwuzhanId;
    }

    public void setFuwuzhanId(Long fuwuzhanId)
    {
        this.fuwuzhanId = fuwuzhanId;
    }

    public String getCustomerName()
    {
        return customerName;
    }

    public void setCustomerName(String customerName)
    {
        this.customerName = customerName;
    }

    public String getCustomerTel()
    {
        return customerTel;
    }

    public void setCustomerTel(String customerTel)
    {
        this.customerTel = customerTel;
    }

    public String getMendianName()
    {
        return mendianName;
    }

    public void setMendianName(String mendianName)
    {
        this.mendianName = mendianName;
    }

    public String getFuwuzhanName()
    {
        return fuwuzhanName;
    }

    public void setFuwuzhanName(String fuwuzhanName)
    {
        this.fuwuzhanName = fuwuzhanName;
    }

}
